﻿using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GloboTicketAdmin.API.Models.Domain
{
    public class EventModel
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = false, ColumnDataType = "TEXT")]
        public Guid Id { get; set; } = Guid.NewGuid(); // 初始化
        [SugarColumn(IsNullable = true)]
        public string? ImageUrl { get; set; }
        [SugarColumn(ColumnName = "Name", Length = 100, IsNullable = false)] // 列名、长度、非空
        public string Name { get; set; } = default!;
        [SugarColumn(ColumnName = "Price", IsNullable = false)]
        public double Price { get; set; }
        [SugarColumn(ColumnName = "Stock", IsNullable = false)]
        public int Stock { get; set; } = 0;
        [SugarColumn(IsNullable = false)]
        [Required]
        public DateTime StartTime { get; set; }

        [SugarColumn(IsNullable = false)]
        [Required]
        public DateTime EndTime { get; set; }
        [SugarColumn(IsNullable = false)]
        public DateTime CreateTime { get; set; } = DateTime.Now;
        [SugarColumn(IsNullable = false)]
        public DateTime UpdateTime { get; set; } = DateTime.Now;
        //[SugarColumn(IsNullable = true)]
        //public List<string> Artists { get; set; } = new();
        // 存储序列化后的 JSON 字符串
        [SugarColumn(IsNullable = true,ColumnDataType = "TEXT")] // SQLite 用 TEXT，其他数据库可用 VARCHAR(MAX) 或 TEXT
        public string? ArtistsJson { get; set; }

        // 非数据库字段，用于程序中操作 List<string>
        [SugarColumn(IsIgnore = true)] // 标记为忽略，不映射到数据库
        public List<string>? Artists
        {
            get
            {
                return string.IsNullOrEmpty(ArtistsJson)
                    ? new List<string>()
                    : JsonConvert.DeserializeObject<List<string>>(ArtistsJson);
            }
            set
            {
                // 序列化为 JSON 字符串
                ArtistsJson = JsonConvert.SerializeObject(value);
            }
        }
        [SugarColumn(IsNullable = true, Length = 500)] // 长度限制
        public string? Description { get; set; }
        [SugarColumn(ColumnName = "EventStatus", IsNullable = false)]
        public EventStatusModel EventStatus { get; set; }
      
        [SugarColumn(ColumnName = "CategoryId", IsNullable = false)]
        public Guid CategoryId { get; set; }
        [SugarColumn(IsIgnore = true)] // 忽略此属性，不映射到数据库
        [Navigate(NavigateType.ManyToOne, nameof(CategoryId))] // 导航属性，建立多对一关系]
        public CategoryModel? Category { get; set; }

    }
}
